﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;

// ReSharper disable once CheckNamespace
namespace kmer.Data {
	/// <summary>
	/// 描述分页数据
	/// </summary>
	public class PaperData<T> {
        /// <summary>
        /// 页码
        /// </summary>
        [Description("页码")]
        public int 页码 { get; protected set; }
        /// <summary>
        /// 页码
        /// </summary>
        [Description("页码")]
        public int Page { get; protected set; }
        /// <summary>
        /// 总共有多少记录
        /// </summary>
        [Description("总共有多少记录")]
        public int 记录总数 { get; protected set; }
        /// <summary>
        /// 总共有多少记录
        /// </summary>
        [Description("总共有多少记录")]
        public int RecordCount { get; protected set; }
        /// <summary>
        /// 每页有多少记录
        /// </summary>
        [Description("每页有多少记录")]
        public int 分页大小 { get; protected set; }
        /// <summary>
        /// 每页有多少记录
        /// </summary>
        [Description("每页有多少记录")]
        public int Limt { get; protected set; }
        /// <summary>
        /// 总共有多少页
        /// </summary>
        [Description("页面总数")]
        public int 页面总数 { get; protected set; }
        /// <summary>
        /// 总共有多少页
        /// </summary>
        [Description("页面总数")]
        public int PageCount { get; protected set; }

        /// <summary>
        /// 跳过的记录数量
        /// </summary>
        [JsonIgnore]
        public int Skip { get; protected set; }
        /// <summary>
        /// 跳过的记录数量
        /// </summary>
        [JsonIgnore]
        public int Top { get; protected set; }
		public IList<T> Items { get; set; }
		/// <summary>
		/// 创建分页数据
		/// </summary>
		/// <see cref="Init"/>
		/// <param name="recordCount">记录总数</param>
		/// <param name="pageSize">分页大小</param>
		/// <param name="pageIndex">页面</param>
		/// <returns>返回一个计算好页面数量的数据类</returns>
		public static PaperData<T> Create(int recordCount, int pageSize = 10, int pageIndex = 1) {
			var r = new PaperData<T> {Items = new List<T>()};
			r.Init(recordCount, pageSize, pageIndex);
			return r;
		}
		/// <summary>
		/// 初始化
		/// 会校验Page和Size并计算PageCount等参数
		/// </summary>
		/// <param name="recordCount"></param>
		/// <param name="pageSize"></param>
		/// <param name="pageIndex"></param>
		public void Init(int recordCount, int pageSize = 10, int pageIndex = 1){
			RecordCount = 记录总数 = recordCount;
			Limt = 分页大小 = Math.Max(pageSize, 1);
			PageCount = 页面总数 = (int)Math.Ceiling((decimal)recordCount / pageSize);
			Page = 页码 = Math.Min(Math.Max(pageIndex, 1), 页面总数);
			Skip = 页码 > 1 ? (页码 - 1) * 分页大小 : 0;
			Top = 页码 * 分页大小;
		}
	}
    /// <summary>
    /// 描述分页数据
    /// </summary>
	public class SplitPagerData {
        /// <summary>
        /// 页码
        /// </summary>
        [Description("页码")]
        public int Page { get; protected set; }
        /// <summary>
        /// 总共有多少记录
        /// </summary>
        [Description("总共有多少记录")]
        public int RecordCount { get; protected set; }
        /// <summary>
        /// 每页有多少记录
        /// </summary>
        [Description("每页有多少记录")]
        public int Limt { get; protected set; }
        /// <summary>
        /// 总共有多少页
        /// </summary>
        [Description("总共有多少页")]
        public int PageCount { get; protected set; }

        /// <summary>
        /// 跳过的记录数量
        /// </summary>
        [JsonIgnore]
        public int Skip { get; protected set; }
        /// <summary>
        /// 跳过的记录数量
        /// </summary>
        [JsonIgnore]
        public int Top { get; protected set; }
		public IEnumerable Items { get; set; }
		/// <summary>
		/// 创建分页数据
		/// </summary>
		/// <see cref="Init"/>
		/// <param name="recordCount">记录总数</param>
		/// <param name="pageSize">分页大小</param>
		/// <param name="pageIndex">页面</param>
		/// <returns>返回一个计算好页面数量的数据类</returns>
		public static SplitPagerData Create(int recordCount, int pageSize = 10, int pageIndex = 1) {
			var r = new SplitPagerData();
			r.Init(recordCount, pageSize, pageIndex);
			return r;
		}
		/// <summary>
		/// 初始化
		/// 会校验Page和Size并计算PageCount等参数
		/// </summary>
		/// <param name="recordCount"></param>
		/// <param name="pageSize"></param>
		/// <param name="pageIndex"></param>
		public void Init(int recordCount, int pageSize = 10, int pageIndex = 1) {
			RecordCount = recordCount;
			Limt = Math.Max(pageSize, 1);
			PageCount = (int)Math.Ceiling((decimal)recordCount / pageSize);
			Page = Math.Min(Math.Max(pageIndex, 1), PageCount);
			Skip = Page > 1 ? (Page - 1) * Limt : 0;
			Top = Page * Limt;
		}
	}
}
